import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path, { resolve } from 'path'
import { terser } from 'rollup-plugin-terser'
import { createHtmlPlugin } from 'vite-plugin-html'

const isDev = process.env.NODE_ENV !== 'production'

const baseConfig = {
	plugins: [vue()],
	resolve: {
		alias: {
			'@/': `${resolve(__dirname, 'packages')}/`
		}
		// conditions: ["import", "require", "browser"]
	},
	server: {
		port: 8000
	},
	build: {
		minify: true,
		lib: {
			entry: path.resolve(__dirname, 'packages/index.js'),
			name: 'form-design',
			formats: ['es'],
			fileName: (format) => `index.${format}.js`
		},
		rollupOptions: {
			// 确保外部化处理那些你不想打包进库的依赖
			external: [
				'vue',
				'ant-design-vue',
				'@ant-design/icons-vue',
				'ant-design-vue/dist/antd.css',
				'lodash-es',
				'vuedraggable-es'
			],
			plugins: [
				terser({
					compress: true
				})
			]
		}
	},

	css: {
		preprocessorOptions: {
			less: {
				modifyVars: {
					'primary-color': '#1890FF',
					'layout-color': '#9867f7'
				},

				// DO NOT REMOVE THIS LINE
				javascriptEnabled: true
			}
		}
	}
}

if (isDev) {
	baseConfig.plugins.push(
		createHtmlPlugin({
			// minify: true,
			entry: 'examples/main.js',
			template: 'public/index.html',
			inject: {
				data: {
					// title: "Snowy2.0"
				}
			}
		})
	)
	baseConfig.build.minify = false
	baseConfig.build.rollupOptions.external = []
}

// https://vitejs.dev/config/
export default defineConfig(baseConfig)
